//
//  Result_popView.m
//  penzer
//
//  Created by caiyc on 2018/9/30.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "Result_popView.h"
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define ALERT_HTIGHT 400
@interface Result_popView()<UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIView *alertview;
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UILabel *messageLab;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *otherBtn;
@property(nonatomic,strong)UIImageView *backImg;
@property(nonatomic,strong)UIImageView *left_Img;
@property(nonatomic,strong)UIImageView *right_Img;
@property(nonatomic,strong)UIButton *left_Btn;
@property(nonatomic,strong)UIButton *right_Btn;
@property(assign)BOOL isreward;
@end
@implementation Result_popView

-(instancetype)init{
    self = [super init];
    if(self){
//        self.frame = [UIScreen mainScreen].bounds;
//        NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"Result_popView"owner:self options:nil];
//        UIView *backView = [nibView objectAtIndex:0];
//        backView.frame = [UIScreen mainScreen].bounds;
//      //  backView.frame = frame;
//        [self addSubview:backView];
        

         self.frame = [UIScreen mainScreen].bounds;
        self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, ALERT_HTIGHT)];
        self.backImg.image = [UIImage imageNamed:@"zhongjaing"];
        //self.backImg.contentMode = UIViewContentModeScaleAspectFit;
        self.alertview.frame = CGRectMake(0, 0, SCREEN_WIDTH-30, ALERT_HTIGHT);
        [self.alertview addSubview:self.backImg];
        self.alertview.center = self.center;
        [self addSubview:self.alertview];

        CGFloat btnSpace = 20;//两个btn之间的间距
        CGFloat btn_w =(SCREEN_WIDTH-15*2-btnSpace)/2;

        self.left_Img = [[UIImageView alloc]initWithFrame:CGRectMake(15, ALERT_HTIGHT-60, btn_w*2, 40)];
        self.left_Img.image = [UIImage imageNamed:@"button chaklan"];
      //  self.left_Img.contentMode = UIViewContentModeScaleAspectFit;
     //   [self.alertview addSubview:self.left_Img];
        
        self.right_Img = [[UIImageView alloc]initWithFrame:CGRectMake(15, ALERT_HTIGHT-60, btn_w*2, 40)];
        self.right_Img.image = [UIImage imageNamed:@"button jixu"];
        self.right_Img.contentMode = UIViewContentModeScaleAspectFit;
        [self.alertview addSubview:self.right_Img];
        
        self.left_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.left_Btn.frame = CGRectMake(15, ALERT_HTIGHT-60, btn_w, 40);
       // self.left_Btn.tag = 0;
        [self.left_Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
     //   [self.alertview addSubview:self.left_Btn];
        
        self.right_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.right_Btn.frame = CGRectMake(15, ALERT_HTIGHT-60, btn_w*2, 40);
        self.right_Btn.tag = 0;
        [self.right_Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertview addSubview:self.right_Btn];


    }
    return self;
       
}

-(void)setResultData:(NSArray *)resultData{
    BOOL isreward = 0;
    self.resultData = resultData;
    NSDictionary *userDic = [XTool GetDefaultInfo:USER_INFO];
    NSString *title = @"抱歉，您未中奖";
    for(int i= 0;i<resultData.count;i++){
        NSDictionary *resultDic = resultData[i];
        if([userDic[@"token"]isEqualToString:resultDic[@"token"]]){
            title = @"恭喜您，中奖啦";
        }
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 100+50*i, screen_width-30, 50)];
        [self.alertview addSubview:view];
        UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 40, 40)];
        images.layer.cornerRadius = 20;
        images.image = [UIImage imageNamed:@"hosts"];
        [view addSubview:images];
        
        UILabel *titles1 = [[UILabel alloc]initWithFrame:CGRectMake(70, 20, 160, 20)];
                    titles1.textAlignment =  1;
                    NSString *name = resultDic[@"member"][@"realname"];
                    NSString *newname = [NSString stringWithFormat:@"%@**", [name substringWithRange:NSMakeRange(0, 1)]];
                    titles1.text = newname;
                   // titles1.text = response[@"data"][0][@"member"][@"realname"];
                    titles1.textColor = [UIColor blackColor];
                    titles1.font = [UIFont systemFontOfSize:15];
                    [self.alertview addSubview:titles1];
        
                    UILabel *titles2 = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 160, 15)];
                    titles2.textAlignment =  1;
                    NSString *mobiles = [NSString stringWithFormat:@"%@",resultDic[@"member"][@"mobile"]];
                    NSString *newmob = [mobiles stringByReplacingCharactersInRange:NSMakeRange(3, 5)  withString:@"*"];
                    titles2.text = newmob;
                    //titles2.text = response[@"data"][0][@"member"][@"mobile"];
                    titles2.textColor = [UIColor blackColor];
        
                    titles2.font = [UIFont systemFontOfSize:15];
                    [self.alertview addSubview:titles2];
        
    }
    UILabel *result_Lab = [[UILabel alloc]initWithFrame:CGRectMake(0, ALERT_HTIGHT-90, screen_width-30, 20)];
    result_Lab.text = title;
    result_Lab.font = [UIFont systemFontOfSize:13];
    result_Lab.textAlignment = NSTextAlignmentCenter;
    [self.alertview addSubview:result_Lab];
}
-(UIView *)alertview
{
    if (_alertview == nil) {
        _alertview = [[UIView alloc] init];
        _alertview.backgroundColor = [UIColor clearColor];
        _alertview.layer.cornerRadius=5.0;
        _alertview.layer.masksToBounds=YES;
        _alertview.userInteractionEnabled=YES;
    }
    return _alertview;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
}
-(void)showWithData:(NSArray *)resultData andisHost:(BOOL )ishost
{
    
    BOOL isreward = 0;
  //  self.resultData = resultData;
    NSDictionary *userDic = [XTool GetDefaultInfo:USER_INFO];
    NSString *title = @"抱歉，您未中奖";
    UIScrollView *botom_Scro = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 140, screen_width-30, 150)];
    //botom_Scro.backgroundColor = [UIColor redColor];
    [self.alertview addSubview:botom_Scro];
    botom_Scro.contentSize = CGSizeMake(0, 75*resultData.count+30);
    botom_Scro.showsVerticalScrollIndicator = 0;
    for(int i= 0;i<resultData.count;i++){
        NSDictionary *resultDic = resultData[i];
        if([userDic[@"token"]isEqualToString:resultDic[@"token"]]){
            title = @"恭喜您，中奖啦";
            self.isreward= 1;
        }else{
           //  title = @"很遗憾，没中奖";
        }
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 70*i, screen_width-30, 50)];
        [botom_Scro addSubview:view];
        UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(100, 10, 50, 50)];
        images.layer.cornerRadius = 25;
        images.layer.masksToBounds = 1;
        NSURL *url = [NSURL URLWithString:resultDic[@"avatar"]];
        [images sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Bitmap.png"]];
        images.contentMode = UIViewContentModeScaleAspectFill;
       // images.image = [UIImage imageNamed:@"hosts"];
        [view addSubview:images];
        
        UILabel *titles1 = [[UILabel alloc]initWithFrame:CGRectMake(160, 15, 160, 20)];
        titles1.textAlignment =  NSTextAlignmentLeft;
        NSString *name = resultDic[@"nickname"];
        NSString *newname = [NSString stringWithFormat:@"%@**", [name substringWithRange:NSMakeRange(0, 1)]];
        titles1.text = newname;
        // titles1.text = response[@"data"][0][@"member"][@"realname"];
        titles1.textColor = [UIColor blackColor];
        titles1.font = [UIFont systemFontOfSize:15];
        [view addSubview:titles1];
        
        UILabel *titles2 = [[UILabel alloc]initWithFrame:CGRectMake(160, 40, 160, 15)];
        titles2.textAlignment =  NSTextAlignmentLeft;
        NSString *mobiles = [NSString stringWithFormat:@"%@",resultDic[@"mobile"]];
        NSString *newmob = [mobiles stringByReplacingCharactersInRange:NSMakeRange(3, 5)  withString:@"****"];
        titles2.text = newmob;
        //titles2.text = response[@"data"][0][@"member"][@"mobile"];
        titles2.textColor = [UIColor blackColor];
        
        titles2.font = [UIFont systemFontOfSize:15];
        [view addSubview:titles2];
        
    }
    UILabel *result_Lab = [[UILabel alloc]initWithFrame:CGRectMake(0, ALERT_HTIGHT-110, screen_width-30, 20)];
    result_Lab.text = title;
    result_Lab.font = [UIFont systemFontOfSize:13];
    result_Lab.textAlignment = NSTextAlignmentCenter;
    [self.alertview addSubview:result_Lab];
    
    if(ishost==1){
        self.isreward = 0;
        self.right_Img.hidden = 1;
        [self.right_Btn  setTitle:@"继续直播" forState:UIControlStateNormal];
        [self.right_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        result_Lab.hidden = 1;
    }else{
        if(self.isreward==1){
            self.right_Img.image = [UIImage imageNamed:@"lingqu.png"];
            
        }
    }
    self.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    self.alertview.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
    self.alertview.alpha = 0;
    [UIView animateWithDuration:0.3 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.4f];
        self.alertview.transform = transform;
        self.alertview.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)btnClick:(UIButton *)btn{
//    if ([self.delegate respondsToSelector:@selector(AlertView:clickedButtonAtIndex:)]) {
//        [self.delegate dxAlertView:self clickedButtonAtIndex:btn.tag];
//    }
    if(self.isreward==1){
    if([self.delegate respondsToSelector:@selector(AlertViews:clickedButtonAtIndex:)]){
        [self.delegate AlertViews:self clickedButtonAtIndex:btn.tag];
    }
    }
    [self dismissAlertView];
}
-(void)dismissAlertView{
    [UIView animateWithDuration:0.3 animations:^{
        [self removeFromSuperview];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
